$(function(){
    initPluploadEvent();
    initKindEditor();
});


/**
 * 初始化上传图片控件
 */
function initPluploadEvent(){
    //初始化轮播图(最多5张图)
    var ids = new Array("img1","img2","img3","img4","img5");
    $(ids).each(function(i,n){
        var self = n;//当前id
        createPlupload(self,'goodsImage',function(data){
            uploadLogoCallback(data,self,function(){
                handleFlag("images-flag",1);
            });
        });
    });
}

/**
 * 图片上传后回调处理,轮播图
 * @param data
 * @param imageId
 * @param callback
 */
function uploadLogoCallback(data,imageId,callback){
    if(data.status == 200 ){
        data = $.parseJSON(data.response);
        var isExistImage = $("#" + imageId + "-hidden").find("input").size() > 0;
        $("#"+imageId).attr("src",imageServer+"/"+data.result[0].originalImageUrl);
        var	innerHtml = '<a href="javascript:void(0);" onclick="clearImage(\''+ imageId +'\');">清空</a>';
        innerHtml += '<input type="hidden" name="imageList[][bigImageUrl]" value="' + data.result[0].bigImageUrl+ '"/>';
        innerHtml += '<input type="hidden" name="imageList[][smallImageUrl]" value="' + data.result[0].smallImageUrl+ '"/>';
        innerHtml += '<input type="hidden" name="imageList[][originalImageUrl]" value="' + data.result[0].originalImageUrl+ '"/>';
        $("#" + imageId + "-hidden").html(innerHtml);

        if (callback && !isExistImage) {
            callback();
        }
    }
}

/**
 * 清空已选择的图片
 * @param imageId
 */
function clearImage(imageId){
    $("#" + imageId).attr("src",appServer+"/images/add_image.png");
    $("#" + imageId + "-hidden").html("");
    if(imageId == "uploadFile"){
        handleFlag("uploadFile-flag",-1);
    } else {
        handleFlag("images-flag",-1);
    }
}

/**
 * 处理图片隐藏域的计数
 * @param flagName
 * @param count
 */
function handleFlag(flagName,count){
    var curCount = $("input[name=" + flagName + "]").val();
    if (!curCount) {
        curCount = "0";
    }
    curCount = parseInt(curCount);
    curCount += count;
    $("input[name=" + flagName + "]").val(curCount);
}

function initKindEditor() {
    // textarea
    var editor = KindEditor.create(
        'textarea[name="goodsDetail"]', {
            items:[
                'source', '|', 'undo', 'redo', '|', 'preview',  'template',  'cut', 'copy', 'paste',
                'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
                'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
                'superscript','|', '/',
                'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold',
                'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image',
                'table', 'hr', 'emoticons', 'pagebreak', 'link', 'unlink','|', '/','clearhtml', 'quickformat', 'selectall', 'fullscreen'
            ],
            uploadJson : appServer+'/goods/kindeditor-image-upload.htm',
            afterBlur : function() {
                this.sync();
            }
        });
}

function changeClassifyId(classifyId) {
    if(classifyId != '') {
        $("#classifyIdCheck option:gt(0)").remove();
        jQuery.ajax({
            type:"post",
            url:appServer + "/goods/ajax/getClassify.json",
            dataType:"json",
            data:"classifyId=" + classifyId,
            success:function(response){
            	if(response.code == 0) {
            		var classifyList = eval('(' + response.data + ')');
            		if(classifyList != null) {
                        for(var i=0;i<classifyList.length;i++ ) {
                            var classify = classifyList[i];
                            $('#classifyIdCheck').append("<option value='" + classify.id + "'>" +classify.name  + "</option>");
                        }
                    }
            	}
            },
            error:function(xhr, message, e){

            }
        });
    }
}

function openCommissionRateWindow(id) {
    window.open(appServer+'/goods/getUser.htm?id='+id, 'newwindow', 'height=400, width=1000, top=20,left=150, toolbar=no, menubar=no, scrollbars=yes, resizable=no,location=no, status=no').focus();
}

function num(obj){
    obj.value = obj.value.replace(/[^\d.]/g,""); //清除"数字"和"."以外的字符
    obj.value = obj.value.replace(/^\./g,""); //验证第一个字符是数字
    obj.value = obj.value.replace(/\.{2,}/g,"."); //只保留第一个, 清除多余的
    obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
    obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); //只能输入两个小数
    if(parseFloat(obj.value)>100){
        obj.value = 100;
    }
}
function checkvalue(obj){
    obj.value = obj.value.replace(/[^\d.]/g,""); //清除"数字"和"."以外的字符
    obj.value = obj.value.replace(/^\./g,""); //验证第一个字符是数字
    obj.value = obj.value.replace(/\.{2,}/g,"."); //只保留第一个, 清除多余的
    obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
    obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); //只能输入两个小数
}

function onlyNum(obj) {
    obj.value = obj.value.replace(/[^\d]/g,""); //清除"数字"和"."以外的字符
    obj.value = obj.value.replace(/^\./g,""); //验证第一个字符是数字
}
